package com.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.StarStudio.Service.UserInfoService;
import com.StarStudio.Utils.DESedeUtils;
import com.StarStudio.Utils.HttpRequestUtils;
import com.StarStudio.Utils.QueryPageUtils;
import com.ruoyi.common.core.Entity.HttpStatusCode;
import com.ruoyi.common.core.Entity.MessageConstant;
import com.ruoyi.common.core.Entity.PageResult;
import com.ruoyi.common.core.Entity.Result;
import com.ruoyi.common.security.annotation.RequiresLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
//@RequestMapping("/user_info")
@Slf4j
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    /**
     * 条件查询 用户信息分页接口
     * @param userInfo 用户信息对象
     * @return 分页结果对象 PageResult
     */
    @RequiresLogin
    @GetMapping("/page")
    public PageResult page(UserInfo userInfo){
        QueryPageUtils.startPage();
        PageInfo pageInfo;
        List<UserInfo> userInfoList;
        long allRecordNumber;
        try {
            userInfoList = userInfoService.findUserInfoList(userInfo);
            allRecordNumber = userInfoService.findAllRecordNumber();
            pageInfo = new PageInfo<>(userInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS,pageInfo.getTotal(), userInfoList, allRecordNumber);
    }

    /**
     * 将通过登录校验的用户信息保存到数据库
     * 如果第一次登录，保存用户所有信息，如果不是第一次登录，更新用户信息（这些信息从省平台获取）
     * @param userInfo 用户信息对象
     * @return 结果对象 Result
     */
    @PostMapping("/post")
    @RequiresLogin
    @SuppressWarnings("deprecation")
    public Result UserInfoSave(@RequestBody UserInfo userInfo){
        String mobile = null;
        try {
            String encodeMobile = userInfo.getMobile();
            if (!StringUtils.isEmpty(encodeMobile)){
                //解密手机号
                mobile = DESedeUtils.decrypt(encodeMobile, HttpRequestUtils.getProvinceSystemToken());
            }
            int byCondition = userInfoService.findByCondition(userInfo);
            if (byCondition != 0){
                UserInfo byUserId = userInfoService.findByUserId(userInfo.getId());//从我们平台的数据库中获取已存在的用户信息
                // 给用户对象更新值
                byUserId.setAvatar(userInfo.getAvatar());
                byUserId.setMobile(mobile);
                byUserId.setBeans(userInfo.getBeans());
                byUserId.setStar(userInfo.getStar());
                byUserId.setLast_login_time(new Timestamp(new Date().getTime()));
                userInfoService.updateUserInfo(byUserId);
            }else {
                userInfo.setMobile(mobile);
                userInfoService.UserInfoSave(userInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.USER_INFO_SAVE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.USER_INFO_SAVE_SUCCESS);
    }

    /**
     * 更新用户信息
     * @param userInfo 用户信息对象
     * @return 结果对象 Result
     */
    @PutMapping("/put")
    public Result updateUserInfo(@RequestBody UserInfo userInfo){
        try {
            userInfoService.updateUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_USER_INFO_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_USER_INFO_SUCCESS);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/findByUserId/{userId}")
    UserInfo findByUser_id(@PathVariable(value = "userId") long userId){
        return userInfoService.findByUserId(userId);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/findAllRecordNumber")
    long findAllRecordNumber(){
        return userInfoService.findAllRecordNumber();
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/TodayLoginUserNumber")
    int today_login_user_number(@RequestParam(value = "today") String today){
        return userInfoService.today_login_user_number(today);
    }

    /*
     *  远程调用接口
     */
    @RequestMapping("/TodayPurchaseUserNumber")
    int today_purchase_user_number(){
        return userInfoService.today_purchase_user_number();
    }

    @RequestMapping("/TotalPurchaseUserNumber")
    int totalPurchaseUserNumber(){
        return userInfoService.totalPurchaseUserNumber();
    }

    @RequestMapping("/BeansSpendToday")
    int Beans_spend_today(){
        return userInfoService.Beans_spend_today();
    }

    @RequestMapping("/BeansSpendTotal")
    int Beans_spend_total(){
        return userInfoService.Beans_spend_total();
    }

    @RequestMapping("/CouponsSpendToday")
    int Coupons_spend_today(){
        return userInfoService.Coupons_spend_today();
    }

    @RequestMapping("/CouponsValueSpendToday")
    double Coupons_value_spend_today(){
        return userInfoService.Coupons_value_spend_today();
    }

    @RequestMapping("/CouponsSpendTotal")
    int Coupons_spend_total(){
        return userInfoService.Coupons_spend_total();
    }

    @RequestMapping("/CouponsValueSpendTotal")
    double Coupons_value_spend_total(){
        return userInfoService.Coupons_value_spend_total();
    }
}
